package com.example.bigwork;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bigwork.adapter.CartAdapter;

import java.util.List;

public class CartActivity extends AppCompatActivity {

    //ListView控件
    private ListView lvshop;
    //购物车数据源
//    private List<Shop> shoCart = new ArrayList<>();
    private List<Shop> shopCart = MainActivity.shoCart;
    //购物车商品数量数据源
    private List<Integer> curnum = MainActivity.curnum;
    //购物车适配器对象
    private CartAdapter cartAdapter;
    //用于接收一条商品信息
    private Shop shop;
    //使用sumPrice表示商品的总价格
    public static double sumPrice = 0.0;
    //商品的总金额空间
    public static TextView sum;
    //绑定购买按钮
    private Button buy;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);


        //获取控件
        getViews();


        //获取带来的数据
        //获取Intent对象
        Intent intent = getIntent();
        //获取intent里面的bundle对象
        Bundle bundle = intent.getBundleExtra("obj");
        //获取bundle里面的数据
        shop = (Shop) bundle.getSerializable("shop");

        //将获取的信息加入到整个购物车信息集合中，添加时去重
        int flag = 0;
        for(Shop shop1:shopCart){
            if (shop.getsDetail().equals(shop1.getsDetail())){
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            shopCart.add(shop);
            MainActivity.curnum.add(1);
        }


        //结算
        //获取商品总金额
        CartActivity.sumPrice=0.0;
        for(int i = 0;i<curnum.size();i++){
            //获取i位置的商品数量
            int counti = curnum.get(i);
            //获取i位置的商品价格
            double pricei = Double.parseDouble(MainActivity.shoCart.get(i).getsPrice().substring(1));
            //计算总金额
            sumPrice = sumPrice + (counti * pricei);
            sumPrice =(double) Math.round(sumPrice*100)/100;
        }
        sum.setText(sumPrice+"");


        //购买
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(CartActivity.this)
                        .setTitle("购买成功")
                        .setMessage("您一共花费"+sumPrice)
                        .setPositiveButton("确定",null)
                        .show();

            }

        });







        //实例化CartAdapter适配器对象
        cartAdapter = new CartAdapter(
                this,
                R.layout.cart_list_item,
                shopCart
        );
        //给ListView绑定适配器
        lvshop.setAdapter(cartAdapter);







    }

    private void getViews() {
        lvshop = findViewById(R.id.cart_list);
        sum = findViewById(R.id.sum_price);
        buy  = findViewById(R.id.settle_shops);
    }

}